import { h, onMounted, reactive, ref, toRaw } from "vue";
import type { PaginationProps } from "@pureadmin/table";
import { deviceDetection } from "@pureadmin/utils";
import { message } from "@/utils/message";
import {
  getPickUpListApi,
  delPickUpApi,
  getPickUpShowApi,
  checkPickUpApi
} from "@/api/pickupApi";
import { addDialog } from "@/components/ReDialog";
import Details from "../detail.vue";
import forms from "../statusForm.vue";
import orderCar from "../orderCar/index.vue";
export function usePickUp() {
  const form = reactive({
    page: 1,
    perPage: 10,
    keyword: null,
    orderType: null
  });

  const dataList = ref([]);
  const isShow = ref(false);
  const loading = ref(true);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  const columns: TableColumnList = [
    {
      label: "订单号",
      prop: "orderNum",
      minWidth: 110
    },
    {
      label: "提货日期",
      prop: "deliveryDate"
    },
    {
      label: "提货类型",
      prop: "orderType",
      formatter(row, column, cellValue) {
        return cellValue === 1 ? "正常提货" : "异常退货";
      }
    },
    {
      label: "公司简称",
      prop: "corp.shortName"
    },
    {
      label: "用户",
      prop: "user.name"
    },
    {
      label: "提货备注",
      prop: "remarks"
    },
    {
      label: "发起人",
      prop: "user.name"
    },
    {
      label: "状态",
      prop: "statusStr"
    },
    {
      label: "创建时间",
      prop: "createdAt",
      minWidth: 100
    },
    {
      label: "操作",
      fixed: "right",
      slot: "operation",
      minWidth: 150
    }
  ];
  function handleSizeChange(val: number) {
    loading.value = true;
    form.page = 1;
    form.perPage = val;
    getPickUpListApi(toRaw(form)).then(result => {
      dataList.value = result.result.list;
      pagination.total = result.result.total;
      pagination.pageSize = result.result.per_page;
      pagination.currentPage = 1;
    });

    setTimeout(() => {
      loading.value = false;
    }, 150);
  }

  function handleCurrentChange(val: number) {
    loading.value = true;
    form.page = val;
    getPickUpListApi(toRaw(form)).then(result => {
      dataList.value = result.result.list;
      pagination.total = result.result.total;
      pagination.pageSize = result.result.per_page;
      pagination.currentPage = result.result.current_page;
    });

    setTimeout(() => {
      loading.value = false;
    }, 150);
  }

  async function onSearch() {
    loading.value = true;
    const { result } = await getPickUpListApi(toRaw(form));
    dataList.value = result.list;
    pagination.total = result.total;
    pagination.pageSize = result.per_page;
    pagination.currentPage = result.current_page;

    setTimeout(() => {
      loading.value = false;
    }, 150);
  }

  const resetForm = formEl => {
    if (!formEl) return;
    form.keyword = null;
    form.orderType = null;
    formEl.resetFields();
    onSearch();
  };
  function onDetail(row) {
    getPickUpShowApi({ deliveryOrderId: row.deliveryOrderId }).then(res => {
      addDialog({
        top: "5vh",
        width: "49%",
        hideFooter: true,
        contentRenderer: () => h(Details),
        props: {
          info: [res.result]
        }
      });
    });
  }
  onMounted(async () => {
    onSearch();
  });

  function handleDelete(row) {
    delPickUpApi({ deliveryOrderId: row.deliveryOrderId }).then(() => {
      message(`删除成功`, { type: "success" });
      onSearch();
    });
  }
  function onFormOneClick(row) {
    addDialog({
      width: "30%",
      title: "审核",
      contentRenderer: () => forms,
      props: {
        // 赋默认值
        formInline: {
          status: row.status
        }
      },
      closeCallBack: ({ options, args }) => {
        const { formInline } = options.props;
        const value = `${formInline.status}`;
        if (args?.command === "sure") {
          checkPickUpApi({
            deliveryOrderId: row.deliveryOrderId,
            status: value
          })
            .then(() => {
              message(`状态修改成功`, { type: "success" });
              onSearch();
            })
            .catch(error => {
              message(error.response.data.msg ?? "操作失败,请联系管理员", {
                type: "error"
              });
              return false;
            });
        }
      }
    });
  }
  async function onOrderCarClick(row) {
    addDialog({
      title: `提货单车辆审核`,
      width: "44%",
      draggable: true,
      fullscreen: deviceDetection(),
      fullscreenIcon: true,
      closeOnClickModal: false,
      contentRenderer: () => h(orderCar, { data: row.deliveryOrderId })
    });
  }
  return {
    form,
    isShow,
    loading,
    columns,
    dataList,
    pagination,
    onSearch,
    resetForm,
    handleSizeChange,
    handleCurrentChange,
    deviceDetection,
    handleDelete,
    onDetail,
    onFormOneClick,
    onOrderCarClick
  };
}
